package com.manger.format;

import com.manger.common.office.sax.ExcelFactory;
import com.manger.model.Project;
import com.manger.model.ThreeSixZero;
import com.manger.utils.CopyFiles;
import com.manger.utils.LoanFileEnum;
import com.manger.utils.SystemConstant;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.formula.functions.T;

import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

/**
 * @ClassName ThreeSixZeroCaseFilesFormat
 * @Description TODO
 * @Author @LiuHongJun
 * @Date 2025/9/1 16:01
 * @Version 1.0
 */
public class ThreeSixZeroCaseFilesFormat implements CaseFilesFormat {

    private List<String> onlyOneFileNameMarks = new ArrayList<>();

    private Map<String, String> idCardMap = new HashMap<>();

    private static Map<String, LoanFileEnum> filesMapping = new HashMap<>();

    {
        onlyOneFileNameMarks.add("人脸照片");
        onlyOneFileNameMarks.add("身份证反面");
        onlyOneFileNameMarks.add("身份证正面");
    }

    @Override
    public void format(String extractDir, String targetDir, Project project) throws Exception {
        System.out.println("++++++++++++++++++++++360案件数据整理+++++++++++++++++++++++++++++");
        List<String> ignoreFileList = project.getIgnoreFilesList();
        List<String> ignoreDirList = project.getIgnoreDirList();
        String excelFilePath = extractDir + "/" + SystemConstant.ENTRUSTED_INFO;
        File excelFile = new File(excelFilePath);
        if (!excelFile.exists()) {
            System.out.println("360委托信息表文件不存在【" + excelFilePath + "】");
            return;
        }
        Map<String, List<?>> map = ExcelFactory.getExcelFactory().getParseExecutor().parse(excelFile, ThreeSixZero.class).get();
        if (map.keySet().size() <= 0) {
            System.out.println("没有解析到委托表数据！【" + excelFilePath + "】");
            return;
        }
        List<ThreeSixZero> threeSixZeroList = (List<ThreeSixZero>) map.get(map.keySet().iterator().next());
        List<ThreeSixZero> errorList = new ArrayList<>();
        boolean flag = true;
        for (ThreeSixZero threeSixZero : threeSixZeroList) {
            threeSixZero.setIdCard(threeSixZero.getIdCard().replaceAll("\t",""));
            String dirName = threeSixZero.getLoanUserName() + "-" + threeSixZero.getIdCard();
            String resourcePath = extractDir + "/" + project.getDebtName() + "/" + threeSixZero.getIdCard() + "-" + threeSixZero.getLoanUserName();
            String tempTargetDir = targetDir + "/" + project.getDebtName() + "/" + SystemConstant.CASE_FILE_DIR_NAME + "/" + dirName;
            try {
                File file = new File(resourcePath);
                if (!file.exists()) {
                    continue;
                }
                CopyFiles.createDirs(tempTargetDir);
                File[] files = file.listFiles();
                for (File tempFile : files) {
                    if (tempFile.isFile() && CopyFiles.hasFileNameMark(tempFile.getName(), ignoreFileList)) {
                        continue;
                    }
                    if (tempFile.isDirectory() && CopyFiles.hasFileNameMark(tempFile.getName(), ignoreDirList)) {
                        continue;
                    }
                    if (!CopyFiles.checkHasCopyFileToDirByOnlyOneFileNameMarks(tempFile.getName(), onlyOneFileNameMarks,tempTargetDir)) {
                        continue;
                    }
                    CopyFiles.copyFileToDir(tempFile.getAbsolutePath(),
                            tempTargetDir + "/" + CopyFiles.getLoanFileEnum(tempFile.getName(),filesMapping).getName());
                }
                idCardMap.put(threeSixZero.getIdCard(), tempTargetDir);
                //拷贝统一数据
                CopyFiles.copyBaseFiles(tempTargetDir, project);
                System.out.println("整理原始数据成功：【" + dirName + "】");
            } catch (Exception e) {
                e.printStackTrace();
                errorList.add(threeSixZero);
                System.out.println("整理原始数据失败：【" + dirName + "】");
            }
        }

        //处理数据u idCardMap : 身份证：地址
        CopyFiles.copyOtherMaterialFileToDir(extractDir,idCardMap);

        if(!errorList.isEmpty()){
            String errorFilePath = targetDir + "/" + project.getDebtName() + "/" + SystemConstant.ERROR_EXCEL_PRE + System.currentTimeMillis() + ".xlsx";
            ExcelFactory.getExcelFactory().getExportExecutor().sheet("整理错误")
                    .data(errorList).head(ThreeSixZero.class).write(new FileOutputStream(errorFilePath));
            System.out.println("导出整理错误数据【"+errorFilePath+"】");
        }
    }

    public static void main(String[] args) throws Exception {
        String extractDir = "C:\\Users\\liuhj\\Desktop\\360";
        String targetDir = "C:\\Users\\liuhj\\Desktop\\案件整理结果";
        Project project = new Project();
        project.setDebtName("360");
        ThreeSixZeroCaseFilesFormat threeSixZeroCaseFilesFormat = new ThreeSixZeroCaseFilesFormat();
        threeSixZeroCaseFilesFormat.format(extractDir,targetDir,project);
    }
}
